package com.eastfair.venuebooking.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.utils.ConvertUtil;
//import com.eastfair.core.utils.SpireDocUtils;
import com.eastfair.file.api.FileServiceFeign;
import com.eastfair.file.vo.FileVO;
import com.eastfair.pay.api.AttachmentFileFeign;
import com.eastfair.venuebooking.dao.TemplateManageMapper;
import com.eastfair.venuebooking.dto.TemplateManagePageQuery;
import com.eastfair.venuebooking.dto.TemplateManageSaveDTO;
import com.eastfair.venuebooking.entity.TemplateManage;
import com.eastfair.venuebooking.service.TemplateManageService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.venuebooking.vo.TemplateManageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.apache.logging.log4j.util.Strings;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import javax.annotation.Resource;

/**
 * <p>
 * 业务实现类
 * 模板管理
 * </p>
 *
 * @author lyw
 * @date 2022-07-05
 */
@Slf4j
@Service
public class TemplateManageServiceImpl extends SuperServiceImpl<TemplateManageMapper, TemplateManage> implements TemplateManageService {
    @Resource
    private AttachmentFileFeign attachmentFileFeign;
    @Resource
    private FileServiceFeign fileServiceFeign;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<TemplateManage> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(TemplateManage model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        return R.successDef();
    }

    /**
     * 模板列表
     *
     * @param params
     * @return
     */
    @Override
    public IPage<TemplateManageVO> getTemplateList(PageParams<TemplateManagePageQuery> params) {
        Page<TemplateManage> page = params.buildPage();
        TemplateManagePageQuery model = params.getModel();
        //查询模板
        QueryWrapper<TemplateManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Strings.isNotBlank(model.getKind()),TemplateManage::getKind, model.getKind())
                .eq(TemplateManage::getIsDeleted, BusinessConstant.DELETE_NO)
                .orderByDesc(TemplateManage::getCreateTime);
        page = page(page, queryWrapper);
        Page<TemplateManageVO> templateManageVOPage = ConvertUtil.convertPage(page, TemplateManageVO.class);
        return templateManageVOPage;
    }

    /**
     * 添加模板
     *
     * @param templateManageSaveDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTemplateInfo(TemplateManageSaveDTO templateManageSaveDTO) {
        TemplateManage templateManage = BeanUtil.toBean(templateManageSaveDTO, TemplateManage.class);
        return this.save(templateManage);
    }

    /**
     * 删除模板
     *
     * @param id
     * @return
     */
    @Override
    public Boolean delTemplateById(Long id) {
        TemplateManage templateManage = new TemplateManage();
        templateManage.setIsDeleted(1);
        templateManage.setId(id);
        return updateById(templateManage);
    }
}
